package com.cskaoyan.controller;

import com.cskaoyan.anno.HashPasswordAnno;
import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Info;
import com.cskaoyan.bean.auth.bo.AuthLoginBo;
import com.cskaoyan.config.shiro.CustomToken;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.util.IpAddressUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author wangdezhao
 * @date 2021/6/5
 * @description 登录验证
 */
@RestController
@RequestMapping("admin/auth/")
public class AdminAuthController {
    @Autowired
    AdminService adminService;

    @RequestMapping("unauth")
    public BaseRespVo unauth() {
        return BaseRespVo.fail("错误,请重新登录!");
    }

    @HashPasswordAnno
    @RequestMapping("login")
    public BaseRespVo login(@RequestBody AuthLoginBo loginBo) {
        Subject subject = SecurityUtils.getSubject();
        String username = loginBo.getUsername();
        String password = loginBo.getPassword();

            try {
                subject.login(new CustomToken(username, password,"admin")); // 整合前后台登录
            } catch (AuthenticationException exception) {
                return BaseRespVo.fail("登录失败，请核对账号信息");
            }

        Serializable id = subject.getSession().getId();
        return BaseRespVo.ok((String) id);
    }

    /**
     * @param
     * @return
     */
    @RequestMapping("info")
    public BaseRespVo info(String token, HttpServletRequest request) {
        Info info = new Info();
        Subject subject = SecurityUtils.getSubject();
        Serializable id = subject.getSession().getId();
        Admin admin = (Admin) subject.getPrincipal();
        ArrayList<String> roles =adminService.getRoles(admin);
        ArrayList<String> perms =adminService.getPerms(admin); // 返回 api

        String remoteAddr = IpAddressUtil.getIpAddress(request);
        Integer adminId = admin.getId();
        adminService.getLastLoginTime(adminId);
        adminService.getLastLoginIp(remoteAddr,adminId);
        if (token!=null && token.equals(id)){
            info.setAvatar(admin.getAvatar());
            info.setName(admin.getUsername());
            info.setPerms(perms);
            info.setRoles(roles);
        }

        return BaseRespVo.ok(info);
    }

    /**
     * 登出
     *
     * @return
     */
    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.logout();
        } catch (Exception e) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok("成功");
    }
}

